package com.jia.leetCode;

public class Pro0064 {
    public int minPathSum(int[][] grid) {
        int rows = grid.length, cols = grid[0].length;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (i == 0 && j == 0) {
                    continue;
                } else if (i == 0) {
                    grid[0][j] += grid[0][j - 1];
                } else if (j == 0) {
                    grid[i][0] += grid[i - 1][0];
                } else {
                    grid[i][j] = Math.min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j];
                }
            }
        }
        return grid[rows - 1][cols - 1];
    }

    public static void main(String[] args) {
        Pro0064 pro0064 = new Pro0064();
        int[][] grid = {{1, 3, 1},
                {1, 5, 1},
                {4, 2, 1}};
        pro0064.minPathSum(grid);
    }
}
